package com.ruge.controller;

import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.ruge.service.GenCodeService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("gencode")
public class GenCodeController {

    @Resource
    private GenCodeService genCodeService;

    @Resource
    private DynamicRoutingDataSource dynamicRoutingDataSource;

    /**
     * 获取所有已配置的数据源名称
     */
    @PostMapping("/list")
    public Map<String, Object> listDataSources() {
        ArrayList<String> list = new ArrayList<>(dynamicRoutingDataSource.getDataSources().keySet());
        Map<String, Object> map = new HashMap<>();
        map.put("data", list);
        map.put("msg", "查询成功");
        return map;
    }

    /**
     * 根据表名生成 Model 类
     *
     * @return 生成结果信息
     */
    @PostMapping("/generate")
    public Map<String, Object> generateModel(@RequestBody Map<String, String> params) {
        return genCodeService.generate(params);
    }
}
